﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Koonsoft.CustomerService.DAL;
using Koonsoft.CustomerService.Entity;

namespace Koonsoft.CustomerService.BLL
{
    public class IssuesBLL
    {
        private IssuesDAL _dal = null;

        protected IssuesDAL DAL
        {
            get
            {
                if (_dal == null)
                {
                    _dal = new IssuesDAL();
                }

                return _dal;
            }
        }

        object obj = new object();

        public bool Create(Issue issue, List<IssueAccessorie> issueAccessories, out string msg)
        {
            issue.Status = new WFBLL().GetFirstWF(out msg).WFID;

            lock (obj)
            {
                string id = DAL.GetNewId("IssueId", 4, String.Format("{0:yyyyMMdd}", DateTime.Today), out msg);

                if (!string.IsNullOrEmpty(msg))
                {
                    msg = "问题编号获取失败：" + msg;
                    return false;
                }
                issue.ID = string.Format("{0}{1}", DateTime.Now.ToString("yyMMdd"), id);
            }

            IssueStateUpdateLog issuelog = new IssueStateUpdateLog();
            issuelog.IssueID = issue.IssueID;
            issuelog.Mark = "";
            issuelog.OldState = issue.Status.ToString();
            issuelog.NewState = issue.Status.ToString();
            issuelog.UserName = issue.CreateUser;
            issuelog.LogType = 0;
            new IssueStateUpdateLogBLL().Create(issuelog, out msg);

            new IssueAccessoriesBLL().Create(issueAccessories);
            return DAL.Create(issue, out msg);
        }

        public bool Update(Issue issue, string loginName, string mark, out string msg)
        {
            IssueStateUpdateLog issuelog = new IssueStateUpdateLog();
            issuelog.IssueID = issue.IssueID;
            issuelog.Mark = mark;
            issuelog.OldState = new WFBLL().GetWfById(issue.Status).WFID.ToString();
            issuelog.NewState = new WFBLL().GetNextWf(issue.Status).WFID.ToString();
            issuelog.UserName = loginName;
            issuelog.LogType = 0;

            new IssueStateUpdateLogBLL().Create(issuelog, out msg);
            issue.Status = new WFBLL().GetNextWf(issue.Status).WFID;
            return DAL.Update(issue, out msg);
        }

        public List<Issue> GetIssues(Issue issue, out string msg, DateTime dtSatrt = new DateTime(), DateTime dtEnd = new DateTime())
        {
            return DAL.GetIssues(issue, out msg, dtSatrt, dtEnd);
        }

        public bool IssuesUpdate(Issue newIssue, List<IssueAccessorie> issueAccessories, out string msg)
        {
            msg = "";
            try
            {

                Issue oldIssue = new IssuesDAL().Get(newIssue.IssueID);
                if (oldIssue != null)
                {
                    newIssue.Status = oldIssue.Status;
                    newIssue.LastTime = oldIssue.LastTime;
                    newIssue.CreateUser = oldIssue.CreateUser;
                    newIssue.ExceptionTime = oldIssue.ExceptionTime;

                    List<IssuesDifferent> differents = oldIssue.GetDifferent(newIssue);

                    StringBuilder sb = new StringBuilder();
                    foreach (var dif in differents)
                    {
                        sb.AppendFormat("属性\"{0}\"由\"{1}\"改为\"{2}\"\n", dif.FieldName, dif.OldValue, dif.NewValue);
                    }
                    if (issueAccessories != null)
                    {
                        foreach (var v in issueAccessories)
                        {
                            sb.AppendFormat("上传\"{0}\"文件\n", v.AccessoryName);
                        }
                    }
                    IssueStateUpdateLog issuelog = new IssueStateUpdateLog();
                    issuelog.IssueID = newIssue.IssueID;
                    issuelog.Mark = sb.ToString();
                    issuelog.OldState = newIssue.Status.ToString();
                    issuelog.NewState = newIssue.Status.ToString();
                    issuelog.UserName = newIssue.CreateUser;
                    issuelog.LogType = 1;
                    new IssueStateUpdateLogBLL().Create(issuelog, out msg);
                    new IssueAccessoriesBLL().Create(issueAccessories);

                    return new IssuesDAL().IssuesUpdate(newIssue, out msg);
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                msg = msg + "\n" + ex.Message + ";" + ex.StackTrace;
                return false;
            }
        }
    }
}
